//
//  PrivateLetterCollectionViewCell.m
//  MJApp
//
//  Created by scjh on 15/5/12.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "PrivateLetterCollectionViewCell.h"

@interface PrivateLetterCollectionViewCell ()
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *descLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UIButton *deleteBtn;
@property (weak, nonatomic) IBOutlet UIButton *repeatBtn;

@end

@implementation PrivateLetterCollectionViewCell

- (void)awakeFromNib {
    // Initialization code
    
    self.headImageView.layer.cornerRadius = CGRectGetWidth(self.headImageView.frame) / 2;
    self.headImageView.clipsToBounds = YES;
    self.deleteBtn.layer.borderColor = [UIColor blackColor].CGColor;
    [self.deleteBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.deleteBtn.layer.borderWidth = 1;
    self.deleteBtn.layer.cornerRadius = CGRectGetHeight(self.deleteBtn.frame) / 2;
    self.repeatBtn.layer.borderColor = [UIColor redColor].CGColor;
    [self.repeatBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.repeatBtn.layer.borderWidth = 1;
    self.repeatBtn.layer.cornerRadius = CGRectGetHeight(self.repeatBtn.frame) / 2;
    self.layer.cornerRadius = 3;
}

- (void)setInfoDic:(NSDictionary *)infoDic {
    
    _infoDic = infoDic;
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kUrl,infoDic[@"head"]]];
    [self.headImageView sd_setImageWithURL:url];
    self.nameLabel.text = infoDic[@"use_name"];
    self.descLabel.text = infoDic[@"content"];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT+0800"];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *createDate = [formatter dateFromString:infoDic[@"create_time"]];
    NSTimeInterval timeInt = fabs([createDate timeIntervalSinceNow]);

    NSString *pre = @"秒钟";
    if (timeInt >= 60) {
        
        timeInt /= 60;
        pre = @"分钟";
    }
    if (timeInt >= 60) {
        
        timeInt /= 60;
        pre = @"小时";
    }
    if (timeInt >= 24) {
        
        timeInt /= 24;
        pre = @"天";
    }
    if (timeInt >= 10 && [pre isEqual:@"天"]) {
        
        self.timeLabel.text = infoDic[@"create_time"];
        
    }else {
        
        self.timeLabel.text = [NSString stringWithFormat:@"%d%@前",(int)timeInt,pre];
    }
    
}

- (IBAction)delete:(id)sender{
    
    if ([self.delegate respondsToSelector:@selector(homeCell:withInfo:)]) {
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.infoDic];
        dic[@"state"] = @"delete";
        [self.delegate homeCell:self withInfo:dic];
    }
}

- (IBAction)repley:(id)sender {
    
    if ([self.delegate respondsToSelector:@selector(homeCell:withInfo:)]) {
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.infoDic];
        dic[@"state"] = @"reply";
        [self.delegate homeCell:self withInfo:dic];
    }
}

@end
